/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ineatconseil.yougo.wicket.bo;

import com.ineatconseil.yougo.dto.RequestTypeDto;
import com.ineatconseil.yougo.wicket.dao.RequestTypesDao;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

/**
 *
 * @author nicolasger
 */
public class SortableRequestTypeProvider extends SortableDataProvider<RequestTypeDto> {

	private static final long serialVersionUID = 1L;
	private RequestTypesDao typeDAO = new RequestTypesDao();

	public SortableRequestTypeProvider() {
		this.setSort("id", true);
	}

	public Iterator<? extends RequestTypeDto> iterator(int first, int count) {
		List<RequestTypeDto> liste = typeDAO.getTypesDemande();
		Collections.sort(liste, new RequestTypeComparator());
		return liste.subList(first, first + count).iterator();
	}

	public int size() {
		return typeDAO.getTypesDemande().size();
	}

	public IModel<RequestTypeDto> model(RequestTypeDto t) {
		return new Model<RequestTypeDto>(t);
	}

	public class RequestTypeComparator implements Comparator<RequestTypeDto> {

		public int compare(RequestTypeDto o1, RequestTypeDto o2) {
			int result = 0;
			if (getSort().getProperty().equals("id")) {
				result = o1.getId().compareTo(o2.getId());
			}
			if (getSort().getProperty().equals("description")) {
				result = o1.getDescription().compareTo(o2.getDescription());
			}

			if (getSort().isAscending()) {
			} else {
				result = -result;
			}
			return result;
		}
	}
}
